package com.ruoyi.wq.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.GiveType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqComment;
import com.ruoyi.wq.domain.WqCommentGive;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.mapper.WqCommentGiveMapper;
import com.ruoyi.wq.service.IWqCommentGiveService;
import com.ruoyi.wq.service.IWqCommentService;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqNoticeGiveService;

/**
 * 评论点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@Service
public class WqCommentGiveServiceImpl extends ServiceImpl<WqCommentGiveMapper, WqCommentGive> implements IWqCommentGiveService {
    
	@Autowired
    private WqCommentGiveMapper wqCommentGiveMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private IWqNoticeGiveService wqNoticeGiveService;
    
    @Autowired
    private IWqCommentService wqCommentService;
    
    @Autowired
    private IWqDynamicService wqDynamicService;

    /**
     * 查询评论点赞
     * 
     * @param id 评论点赞主键
     * @return 评论点赞
     */
    @Override
    public WqCommentGive selectWqCommentGiveById(String id)
    {
        return wqCommentGiveMapper.selectWqCommentGiveById(id);
    }

    /**
     * 查询评论点赞列表
     * 
     * @param wqCommentGive 评论点赞
     * @return 评论点赞
     */
    @Override
    public List<WqCommentGive> selectWqCommentGiveList(WqCommentGive wqCommentGive)
    {
        return wqCommentGiveMapper.selectWqCommentGiveList(wqCommentGive);
    }

    /**
     * 新增评论点赞
     * 
     * @param wqCommentGive 评论点赞
     * @return 结果
     */
    @Override
    public Boolean insertWqCommentGive(WqCommentGive wqCommentGive){
    	// 是否点赞
    	Boolean isGive = false;
    	
    	SysUser sysUser = sysUserService.getUserInfo(SecurityUtils.getUserId());
    	WqComment comment = wqCommentService.getById(wqCommentGive.getCommentId());
    	if(sysUser.getUserId().equals(comment.getUserId())){
    		// 不能给自己点赞
    		return true;
    	}
    	WqDynamic dynamic = wqDynamicService.getById(comment.getDynamicId());
    	WqCommentGive give = getWqCommentGive(wqCommentGive.getCommentId(), sysUser.getUserId());
    	if(give == null){
    		isGive = true;
    		wqCommentGive.setUserId(sysUser.getUserId());
    		wqCommentGive.setAvatar(sysUser.getAvatar());
    		wqCommentGive.setNickName(sysUser.getNickName());
    		wqCommentGive.setPhone(sysUser.getPhonenumber());
    		wqCommentGive.setSex(sysUser.getSex());
        	baseMapper.insert(wqCommentGive);
        	
        	// 点赞通知
        	WqNoticeGive noticeGive = new WqNoticeGive();
        	noticeGive.setDynamicId(dynamic.getId());
        	if(StringUtils.isNotBlank(dynamic.getImage())){
        		noticeGive.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
        	noticeGive.setCommentId(comment.getId());
        	noticeGive.setGiveType(GiveType.COMMENT_GIVE.getCode());
        	
        	noticeGive.setUserId(sysUser.getUserId());
        	noticeGive.setAvatar(sysUser.getAvatar());
        	noticeGive.setNickName(sysUser.getNickName());
        	noticeGive.setPhone(sysUser.getPhonenumber());
        	noticeGive.setSex(sysUser.getSex());
        	
        	noticeGive.setNoticeUserId(comment.getUserId());
        	wqNoticeGiveService.save(noticeGive);
    	}else{
    		isGive = false;
    		baseMapper.deleteById(give.getId());
    		wqNoticeGiveService.deleteByCommentIdAndUserId(comment.getId(), sysUser.getUserId());
    	}
    	// 统计动态点赞数量
    	Integer giveNum = getGiveNumByCommentId(wqCommentGive.getCommentId());
    	
    	// 更新评论点赞数量
    	comment.setGiveNum(giveNum);
    	wqCommentService.updateById(comment);
    	
    	// 更新用户未读消息数量
    	SysUser user = sysUserService.getById(comment.getUserId());
    	Integer giveNewNum = wqNoticeGiveService.getUnReadNum(comment.getUserId());
    	user.setGiveNewNum(giveNewNum);
    	sysUserService.updateById(user);
    	return isGive;
    }
    
    private WqCommentGive getWqCommentGive(String commentId, Long userId){
    	QueryWrapper<WqCommentGive> qw = new QueryWrapper<>();
    	WqCommentGive entity = new WqCommentGive();
    	entity.setCommentId(commentId);
    	entity.setUserId(userId);
    	qw.setEntity(entity);
    	return baseMapper.selectOne(qw);
    }
    
    private Integer getGiveNumByCommentId(String commentId){
    	QueryWrapper<WqCommentGive> qw = new QueryWrapper<>();
    	WqCommentGive entity = new WqCommentGive();
    	entity.setCommentId(commentId);
    	entity.setStatus("0");
    	qw.setEntity(entity);
    	return baseMapper.selectCount(qw);
    }

    /**
     * 修改评论点赞
     * 
     * @param wqCommentGive 评论点赞
     * @return 结果
     */
    @Override
    public int updateWqCommentGive(WqCommentGive wqCommentGive)
    {
        wqCommentGive.setUpdateTime(DateUtils.getNowDate());
        return wqCommentGiveMapper.updateWqCommentGive(wqCommentGive);
    }

    /**
     * 批量删除评论点赞
     * 
     * @param ids 需要删除的评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentGiveByIds(String[] ids)
    {
        return wqCommentGiveMapper.deleteWqCommentGiveByIds(ids);
    }

    /**
     * 删除评论点赞信息
     * 
     * @param id 评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentGiveById(String id)
    {
        return wqCommentGiveMapper.deleteWqCommentGiveById(id);
    }
}
